package xyz.quanguanjia.masterboy.me;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.luck.picture.lib.utils.ToastUtils;

import java.util.HashMap;

import xyz.quanguanjia.masterboy.MBDHttpClient;
import xyz.quanguanjia.masterboy.MBDModelResponse;
import xyz.quanguanjia.masterboy.MBDToolbarUtils;
import xyz.quanguanjia.masterboy.MBDUserCenter;
import xyz.quanguanjia.masterboy.R;
import xyz.quanguanjia.masterboy.ServiceType;
import xyz.quanguanjia.masterboy.model.MBDAccountInfoModel;
import xyz.quanguanjia.masterboy.util.StringUtils;


public class MBDEditInfoActivity extends AppCompatActivity {


    private EditText contentEt;
    private String content, key;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_info);
        contentEt = findViewById(R.id.content_et);
        TextView number_tv = findViewById(R.id.number_tv);
        //导航栏
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        content = intent.getStringExtra("content");
        key = intent.getStringExtra("key");
        if (!StringUtils.isNull(content)) {
            contentEt.setText(content);
            number_tv.setText(content.length() + "/30");
        } else {
            number_tv.setText("0/30");
        }
        contentEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 在这里处理文本变化之前的事情
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 在这里处理文本变化时的事情
                // 例如，你可以在这里更新UI或执行一些逻辑
            }

            @Override
            public void afterTextChanged(Editable s) {
                // 在这里处理文本变化之后的事情
                // 注意：这个方法会在主线程调用，如果处理逻辑较重，请考虑使用异步处理
                number_tv.setText(s.length() + "/30");
            }
        });

        MBDToolbarUtils.setCenterTitle(toolbar, title);
        MBDToolbarUtils.backButtonClicked(toolbar, new Runnable() {
            @Override
            public void run() {
                finish();
            }
        });
        MBDToolbarUtils.toolButtonClicked(toolbar, R.id.img_toolbar_more, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateUserInfo(contentEt.getText().toString());
            }
        });
    }


    private void updateUserInfo(String content) {
        if (TextUtils.isEmpty(content)) {
            ToastUtils.showToast(MBDEditInfoActivity.this, "请输入信息");
            return;
        }
        String userId = MBDAccountInfoModel.current().getUserId();
        if (TextUtils.isEmpty(userId)) {
            MBDUserCenter.shared().setToken("");
            MBDAccountInfoModel.clear();
            Toast.makeText(MBDEditInfoActivity.this, "请重新登录！", Toast.LENGTH_SHORT).show();
            return;
        }

        HashMap<String, String> params = new HashMap<String, String>();
        params.put(key, content);
        params.put("userId", userId);

        MBDHttpClient.shared().doRequest(ServiceType.SERVICE_USER_UPDATE_INFO, params, new MBDHttpClient.Callback() {
            @Override
            public void completion(boolean success, Error error, MBDModelResponse response) {
                if (success) {
                    Toast.makeText(MBDEditInfoActivity.this, "更新成功！", Toast.LENGTH_SHORT).show();
                    MBDAccountInfoModel.update(true);
                    finish();
                } else {
                    Toast.makeText(MBDEditInfoActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
